package com.ruoyi.hospital.order.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.hospital.hospitalized.domain.THospitalized;
import com.ruoyi.hospital.order.domain.TOrder;
import com.ruoyi.hospital.order.mapper.TOrderMapper;
import com.ruoyi.hospital.order.service.ITOrderService;
import com.ruoyi.hospital.registered.domain.TRegistered;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 订单管理Service业务层处理
 *
 * @author Shawn
 * @date 2024-02-22
 */
@Service
public class TOrderServiceImpl implements ITOrderService {
    @Autowired
    private TOrderMapper tOrderMapper;

    /**
     * 查询订单管理
     *
     * @param id 订单管理主键
     * @return 订单管理
     */
    @Override
    public TOrder selectTOrderById(Long id) {
        return tOrderMapper.selectTOrderById(id);
    }

    /**
     * 查询订单管理列表
     *
     * @param tOrder 订单管理
     * @return 订单管理
     */
    @Override
    public List<TOrder> selectTOrderList(TOrder tOrder) {
        return tOrderMapper.selectTOrderList(tOrder);
    }

    /**
     * 新增订单管理
     *
     * @param tOrder 订单管理
     * @return 结果
     */
    @Override
    public int insertTOrder(TOrder tOrder) {
        tOrder.setCreatedTime(new Date());
        tOrder.setCreatedBy(String.valueOf(ShiroUtils.getUserId()));
        return tOrderMapper.insertTOrder(tOrder);
    }

    /**
     * 添加门诊订单
     *
     * @param tRegistered
     */
    public int insertRegisteredOrder(TRegistered tRegistered) {
        TOrder tOrder = new TOrder();
        tOrder.setType("1");
        tOrder.setAmount(tRegistered.getPrice());
        tOrder.setRegisteredId(String.valueOf(tRegistered.getId()));
        tOrder.setPatientId(String.valueOf(tRegistered.getPatientId()));
        tOrder.setComplete(1l);
        return insertTOrder(tOrder);
    }

    /**
     * 添加住院订单
     *
     * @param tHospitalized
     */
    public int insertHospitalizedOrder(THospitalized tHospitalized) {
        TOrder tOrder = new TOrder();
        tOrder.setType("2");
        tOrder.setHospitalizedId(String.valueOf(tHospitalized.getId()));
        tOrder.setPatientId(tHospitalized.getPatientId());
        tOrder.setAmount(tHospitalized.getSettlementAmount());
        tOrder.setComplete(1l);
        return insertTOrder(tOrder);
    }

    /**
     * 添加药品
     *
     * @param price
     * @param tRegistered
     * @return
     */
    public int insertMedicationOrder(BigDecimal price, TRegistered tRegistered) {
        TOrder tOrder = new TOrder();
        tOrder.setType("3");
        tOrder.setRegisteredId(String.valueOf(tRegistered.getId()));
        tOrder.setPatientId(String.valueOf(tRegistered.getPatientId()));
        tOrder.setAmount(price);
        tOrder.setComplete(1l);
        return insertTOrder(tOrder);
    }

    /**
     * 修改订单管理
     *
     * @param tOrder 订单管理
     * @return 结果
     */
    @Override
    public int updateTOrder(TOrder tOrder) {
        return tOrderMapper.updateTOrder(tOrder);
    }

    /**
     * 批量删除订单管理
     *
     * @param ids 需要删除的订单管理主键
     * @return 结果
     */
    @Override
    public int deleteTOrderByIds(String ids) {
        return tOrderMapper.deleteTOrderByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除订单管理信息
     *
     * @param id 订单管理主键
     * @return 结果
     */
    @Override
    public int deleteTOrderById(Long id) {
        return tOrderMapper.deleteTOrderById(id);
    }

}
